<?php
class Chanel_IndexController  extends Zendvn_Controller_Action{
	
	public function init()
	{
		parent::init();
	
		$this->view->main = TRUE;
		$this->view->module = "landpage";
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();

		
		$this->createTemplate(APPLICATION_PATH . '/templates/public','template.ini','template');
	}
	
	public function  indexAction(){
		$this->view->module = "profile";
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		$auth = new Registration_Model_Auth();		
				
		$this->view->module = "profile";
		$id = $this->_request->getParam('id');
		$pid = $this->_request->getParam('pid');
		
		
		if(isset($id)){				
			
			$post = new Post_Model_Index();
			
			$media = new Post_Model_Media();
			
			$comment = new Post_Model_Comment();
			
			$auth = new Registration_Model_Auth();
			
			$like = new Post_Model_Like();
			
			$chanel = new Chanel_Model_Index();
			
			$status = "publish";
			
			$this->view->pagecomment = 1;
			
			$this->view->pagesizecomment = 3;				

			$this->view->chanel = $chanel->getChanelById($id);			 
			
			$page = 1;
			
			$pagesize = 8;
			
			$this->view->page = $page +1;
			
			$this->view->pagesize = $pagesize;
			
			$this->view->id = $id;
			
			$this->view->pid = $pid;
			
			$arr = array(
				"chanelid"=>$id,						
				"status"=>$status,	
				"page"=> $page,
				"pagesize"=> $pagesize,
																
			);
			
			if(isset($pid)){				
				$this->view->post_item = $post->getItem($pid);
				$this->view->posts =$post->listItemId($arr,$pid);
			}else{

				$this->view->posts =$post->listItemChanel($arr);
				
			}
			
			$this->view->total = $post->countPostChanel($arr);			
			
			
			$this->view->media = $media;
			
			$this->view->comment = $comment;
			
			$this->view->auth = $auth;
			
			$this->view->like = $like;
			
			$category = new Category_Model_Index();
			
			$this->view->list_category = $category->listItem();
			
			$this->view->followchanel = new Chanel_Model_FollowChanel();
			
			$this->view->like_chanel = new Chanel_Model_Like();
						
		}
		
	
		
	}
	
	public function uploadcoverAction(){
		
		if($this->getRequest()->isPost()){
			
			$this->view->main = FALSE;
			
			$type = @$_POST["type"];
			
			$id = @$_POST["idchanel"];
			
			if($type == "uploadcover"){
				
				$storage = new Zend_Auth_Storage_Session();
				$data = $storage->read();
	    		$chanel = new Chanel_Model_Index();
				$upload = new Zend_File_Transfer_Adapter_Http();
								
				$path = realpath(APPLICATION_PATH."/../upload/");
				
				$upload->setDestination($path);
				
				$file = $upload->getFileInfo();
				 
				$file_name = $upload->getFilename();
				  
				preg_match("/\.([^\.]+)$/", $file_name, $matches); 
		        $file_ext = $matches[1];
		        $file_name = time() . '.' . $file_ext;
		        $upload->addFilter('Rename', $path .'/' . $file_name);
				
				if (!$upload->isValid()) 
				{
				    throw new Exception('Bad image data: '.implode(',', $upload->getMessages()));
				}
				
				try {
					
			        $upload->receive();
			        
			        
			        
			        $arr = array(
			        	"id"=>$id,
						"userid"=>$data->id,					
						"logo"=>"",			
			        	"name"=>"",
			        	"create_date"=>"",
			        	"type"=>"",
			        	"location"=>"",
			        	"description"=>"",
			        	"cover"=>$file_name,
					);
					
					
					$id = $chanel->updateChanel($arr);
					
					
					
					echo $file_name;
				        
<<<<<<< .mine
=======
				        
				        
				        $arr = array(
						"id"=>$data->id,
						"displayname"=>$data->displayname,
						"password"=>$data->password,
						"avatar"=>$file_name,				
				        "company"=>"",
				        "school"=>"",
				        "home"=>"",
				        "interest"=>"",
				        "cover"=>"",
						);

						$data->avatar = $file_name;
						
						$user->updateProfile($arr);
						
						
				        
>>>>>>> .r88
				} 
				catch (Zend_File_Transfer_Exception $e) 
				{
				        throw new Exception('Bad image data: '.$e->getMessage());
				}
				
			}	
			
<<<<<<< .mine
=======
			
			
			
			}
			
			
				
>>>>>>> .r88
		}
		
	}
	
	public function updatefollowAction(){
	
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		$follow = new Chanel_Model_FollowChanel();
		
		$followfriend = new Registration_Model_Follow();
		
		if($this->getRequest()->isPost()){	

			$id = @$_POST["id"];
			
			$type = @$_POST["type"];
			
			if($type == "followchanel"){
				if ($id != "" && $data){
	
					$arr = array(
						"follower"=>$data->id,						
						"followed"=>$id,	
						"createdate"=>date('Y-m-d h:i:s',time()),
																	
					);
					
					$follow->insertFollow($arr);
							
				}	
			}else{
				
				if ($id != "" && $data){
	
					$arr = array(
						"follower"=>$data->id,						
						"followed"=>$id,	
						"createdate"=>date('Y-m-d h:i:s',time()),																	
					);
					
					$followfriend->insertFollow($arr);
							
				}
				
			}
			
		}
		
		
	}
	
	public function  searchAction(){
		$this->view->main = FALSE;
		if($this->getRequest()->isPost()){
			$val = @$_POST["chanel"];
			$type = @$_POST["type"];
			$chanel = new Chanel_Model_Index();
			$auth = new Registration_Model_Auth();
			
			
			
			if($type == "chanel"){

				if($val != ""){
					$this->view->list_chanel = $chanel->findItem($val);
				}				
			}
			else{
				if($val != ""){	
												
					$this->view->list_user = $auth->findItem($val);
				}
			}
			
			$this->view->type = $type;
			
		}
	}
	
}